#include <iostream>
#include <algorithm>

int const maxn = 5100;
int a[maxn];
int n;

int dfs(int l, int r)
{
	int min = 1 << 30, count = 0;
	for (int i = l; i <= r; i++) min = std::min(min, a[i]);
	for (int i = l; i <= r; i++) a[i] -= min;
	count += min;
	int t = l;
	for(int i = l; i <= r; i++)
		if (!a[i]) { count += dfs(t,i - 1); t = i + 1; }
	if (t <= r) count += dfs(t, r);
	return std::min(count, r - l + 1);
}

int main()
{
	std::cin >> n;
	for (int i = 1; i <= n; i++) std::cin >> a[i];
	std::cout << dfs(1, n) << '\n';
}

